/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import bean.Assurance;
import bean.Optionsupplimentaire;
import bean.Promotion;
import bean.Vignite;
import java.util.ArrayList;
import java.util.List;
import bean.Visitetechnique;
import bean.Panne;
import bean.Vehicule;
import dao.exceptions.IllegalOrphanException;
import dao.exceptions.NonexistentEntityException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author mezouari
 */
public class VehiculeJpaController implements Serializable {

    public VehiculeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Vehicule vehicule) {
        if (vehicule.getVigniteList() == null) {
            vehicule.setVigniteList(new ArrayList<Vignite>());
        }
        if (vehicule.getVisitetechniqueList() == null) {
            vehicule.setVisitetechniqueList(new ArrayList<Visitetechnique>());
        }
        if (vehicule.getPanneList() == null) {
            vehicule.setPanneList(new ArrayList<Panne>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Assurance assurance = vehicule.getAssurance();
            if (assurance != null) {
                assurance = em.getReference(assurance.getClass(), assurance.getId());
                vehicule.setAssurance(assurance);
            }
            Optionsupplimentaire idOption = vehicule.getIdOption();
            if (idOption != null) {
                idOption = em.getReference(idOption.getClass(), idOption.getId());
                vehicule.setIdOption(idOption);
            }
            Promotion idPromotion = vehicule.getIdPromotion();
            if (idPromotion != null) {
                idPromotion = em.getReference(idPromotion.getClass(), idPromotion.getId());
                vehicule.setIdPromotion(idPromotion);
            }
            List<Vignite> attachedVigniteList = new ArrayList<Vignite>();
            for (Vignite vigniteListVigniteToAttach : vehicule.getVigniteList()) {
                vigniteListVigniteToAttach = em.getReference(vigniteListVigniteToAttach.getClass(), vigniteListVigniteToAttach.getId());
                attachedVigniteList.add(vigniteListVigniteToAttach);
            }
            vehicule.setVigniteList(attachedVigniteList);
            List<Visitetechnique> attachedVisitetechniqueList = new ArrayList<Visitetechnique>();
            for (Visitetechnique visitetechniqueListVisitetechniqueToAttach : vehicule.getVisitetechniqueList()) {
                visitetechniqueListVisitetechniqueToAttach = em.getReference(visitetechniqueListVisitetechniqueToAttach.getClass(), visitetechniqueListVisitetechniqueToAttach.getId());
                attachedVisitetechniqueList.add(visitetechniqueListVisitetechniqueToAttach);
            }
            vehicule.setVisitetechniqueList(attachedVisitetechniqueList);
            List<Panne> attachedPanneList = new ArrayList<Panne>();
            for (Panne panneListPanneToAttach : vehicule.getPanneList()) {
                panneListPanneToAttach = em.getReference(panneListPanneToAttach.getClass(), panneListPanneToAttach.getId());
                attachedPanneList.add(panneListPanneToAttach);
            }
            vehicule.setPanneList(attachedPanneList);
            em.persist(vehicule);
            if (assurance != null) {
                Vehicule oldImmatriculationOfAssurance = assurance.getImmatriculation();
                if (oldImmatriculationOfAssurance != null) {
                    oldImmatriculationOfAssurance.setAssurance(null);
                    oldImmatriculationOfAssurance = em.merge(oldImmatriculationOfAssurance);
                }
                assurance.setImmatriculation(vehicule);
                assurance = em.merge(assurance);
            }
            if (idOption != null) {
                idOption.getVehiculeList().add(vehicule);
                idOption = em.merge(idOption);
            }
            if (idPromotion != null) {
                idPromotion.getVehiculeList().add(vehicule);
                idPromotion = em.merge(idPromotion);
            }
            for (Vignite vigniteListVignite : vehicule.getVigniteList()) {
                Vehicule oldIdVehiculeOfVigniteListVignite = vigniteListVignite.getIdVehicule();
                vigniteListVignite.setIdVehicule(vehicule);
                vigniteListVignite = em.merge(vigniteListVignite);
                if (oldIdVehiculeOfVigniteListVignite != null) {
                    oldIdVehiculeOfVigniteListVignite.getVigniteList().remove(vigniteListVignite);
                    oldIdVehiculeOfVigniteListVignite = em.merge(oldIdVehiculeOfVigniteListVignite);
                }
            }
            for (Visitetechnique visitetechniqueListVisitetechnique : vehicule.getVisitetechniqueList()) {
                Vehicule oldIdVehiculeOfVisitetechniqueListVisitetechnique = visitetechniqueListVisitetechnique.getIdVehicule();
                visitetechniqueListVisitetechnique.setIdVehicule(vehicule);
                visitetechniqueListVisitetechnique = em.merge(visitetechniqueListVisitetechnique);
                if (oldIdVehiculeOfVisitetechniqueListVisitetechnique != null) {
                    oldIdVehiculeOfVisitetechniqueListVisitetechnique.getVisitetechniqueList().remove(visitetechniqueListVisitetechnique);
                    oldIdVehiculeOfVisitetechniqueListVisitetechnique = em.merge(oldIdVehiculeOfVisitetechniqueListVisitetechnique);
                }
            }
            for (Panne panneListPanne : vehicule.getPanneList()) {
                Vehicule oldIdVehiculeOfPanneListPanne = panneListPanne.getIdVehicule();
                panneListPanne.setIdVehicule(vehicule);
                panneListPanne = em.merge(panneListPanne);
                if (oldIdVehiculeOfPanneListPanne != null) {
                    oldIdVehiculeOfPanneListPanne.getPanneList().remove(panneListPanne);
                    oldIdVehiculeOfPanneListPanne = em.merge(oldIdVehiculeOfPanneListPanne);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Vehicule vehicule) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Vehicule persistentVehicule = em.find(Vehicule.class, vehicule.getId());
            Assurance assuranceOld = persistentVehicule.getAssurance();
            Assurance assuranceNew = vehicule.getAssurance();
            Optionsupplimentaire idOptionOld = persistentVehicule.getIdOption();
            Optionsupplimentaire idOptionNew = vehicule.getIdOption();
            Promotion idPromotionOld = persistentVehicule.getIdPromotion();
            Promotion idPromotionNew = vehicule.getIdPromotion();
            List<Vignite> vigniteListOld = persistentVehicule.getVigniteList();
            List<Vignite> vigniteListNew = vehicule.getVigniteList();
            List<Visitetechnique> visitetechniqueListOld = persistentVehicule.getVisitetechniqueList();
            List<Visitetechnique> visitetechniqueListNew = vehicule.getVisitetechniqueList();
            List<Panne> panneListOld = persistentVehicule.getPanneList();
            List<Panne> panneListNew = vehicule.getPanneList();
            List<String> illegalOrphanMessages = null;
            if (assuranceOld != null && !assuranceOld.equals(assuranceNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Assurance " + assuranceOld + " since its immatriculation field is not nullable.");
            }
            for (Vignite vigniteListOldVignite : vigniteListOld) {
                if (!vigniteListNew.contains(vigniteListOldVignite)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Vignite " + vigniteListOldVignite + " since its idVehicule field is not nullable.");
                }
            }
            for (Visitetechnique visitetechniqueListOldVisitetechnique : visitetechniqueListOld) {
                if (!visitetechniqueListNew.contains(visitetechniqueListOldVisitetechnique)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Visitetechnique " + visitetechniqueListOldVisitetechnique + " since its idVehicule field is not nullable.");
                }
            }
            for (Panne panneListOldPanne : panneListOld) {
                if (!panneListNew.contains(panneListOldPanne)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Panne " + panneListOldPanne + " since its idVehicule field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (assuranceNew != null) {
                assuranceNew = em.getReference(assuranceNew.getClass(), assuranceNew.getId());
                vehicule.setAssurance(assuranceNew);
            }
            if (idOptionNew != null) {
                idOptionNew = em.getReference(idOptionNew.getClass(), idOptionNew.getId());
                vehicule.setIdOption(idOptionNew);
            }
            if (idPromotionNew != null) {
                idPromotionNew = em.getReference(idPromotionNew.getClass(), idPromotionNew.getId());
                vehicule.setIdPromotion(idPromotionNew);
            }
            List<Vignite> attachedVigniteListNew = new ArrayList<Vignite>();
            for (Vignite vigniteListNewVigniteToAttach : vigniteListNew) {
                vigniteListNewVigniteToAttach = em.getReference(vigniteListNewVigniteToAttach.getClass(), vigniteListNewVigniteToAttach.getId());
                attachedVigniteListNew.add(vigniteListNewVigniteToAttach);
            }
            vigniteListNew = attachedVigniteListNew;
            vehicule.setVigniteList(vigniteListNew);
            List<Visitetechnique> attachedVisitetechniqueListNew = new ArrayList<Visitetechnique>();
            for (Visitetechnique visitetechniqueListNewVisitetechniqueToAttach : visitetechniqueListNew) {
                visitetechniqueListNewVisitetechniqueToAttach = em.getReference(visitetechniqueListNewVisitetechniqueToAttach.getClass(), visitetechniqueListNewVisitetechniqueToAttach.getId());
                attachedVisitetechniqueListNew.add(visitetechniqueListNewVisitetechniqueToAttach);
            }
            visitetechniqueListNew = attachedVisitetechniqueListNew;
            vehicule.setVisitetechniqueList(visitetechniqueListNew);
            List<Panne> attachedPanneListNew = new ArrayList<Panne>();
            for (Panne panneListNewPanneToAttach : panneListNew) {
                panneListNewPanneToAttach = em.getReference(panneListNewPanneToAttach.getClass(), panneListNewPanneToAttach.getId());
                attachedPanneListNew.add(panneListNewPanneToAttach);
            }
            panneListNew = attachedPanneListNew;
            vehicule.setPanneList(panneListNew);
            vehicule = em.merge(vehicule);
            if (assuranceNew != null && !assuranceNew.equals(assuranceOld)) {
                Vehicule oldImmatriculationOfAssurance = assuranceNew.getImmatriculation();
                if (oldImmatriculationOfAssurance != null) {
                    oldImmatriculationOfAssurance.setAssurance(null);
                    oldImmatriculationOfAssurance = em.merge(oldImmatriculationOfAssurance);
                }
                assuranceNew.setImmatriculation(vehicule);
                assuranceNew = em.merge(assuranceNew);
            }
            if (idOptionOld != null && !idOptionOld.equals(idOptionNew)) {
                idOptionOld.getVehiculeList().remove(vehicule);
                idOptionOld = em.merge(idOptionOld);
            }
            if (idOptionNew != null && !idOptionNew.equals(idOptionOld)) {
                idOptionNew.getVehiculeList().add(vehicule);
                idOptionNew = em.merge(idOptionNew);
            }
            if (idPromotionOld != null && !idPromotionOld.equals(idPromotionNew)) {
                idPromotionOld.getVehiculeList().remove(vehicule);
                idPromotionOld = em.merge(idPromotionOld);
            }
            if (idPromotionNew != null && !idPromotionNew.equals(idPromotionOld)) {
                idPromotionNew.getVehiculeList().add(vehicule);
                idPromotionNew = em.merge(idPromotionNew);
            }
            for (Vignite vigniteListNewVignite : vigniteListNew) {
                if (!vigniteListOld.contains(vigniteListNewVignite)) {
                    Vehicule oldIdVehiculeOfVigniteListNewVignite = vigniteListNewVignite.getIdVehicule();
                    vigniteListNewVignite.setIdVehicule(vehicule);
                    vigniteListNewVignite = em.merge(vigniteListNewVignite);
                    if (oldIdVehiculeOfVigniteListNewVignite != null && !oldIdVehiculeOfVigniteListNewVignite.equals(vehicule)) {
                        oldIdVehiculeOfVigniteListNewVignite.getVigniteList().remove(vigniteListNewVignite);
                        oldIdVehiculeOfVigniteListNewVignite = em.merge(oldIdVehiculeOfVigniteListNewVignite);
                    }
                }
            }
            for (Visitetechnique visitetechniqueListNewVisitetechnique : visitetechniqueListNew) {
                if (!visitetechniqueListOld.contains(visitetechniqueListNewVisitetechnique)) {
                    Vehicule oldIdVehiculeOfVisitetechniqueListNewVisitetechnique = visitetechniqueListNewVisitetechnique.getIdVehicule();
                    visitetechniqueListNewVisitetechnique.setIdVehicule(vehicule);
                    visitetechniqueListNewVisitetechnique = em.merge(visitetechniqueListNewVisitetechnique);
                    if (oldIdVehiculeOfVisitetechniqueListNewVisitetechnique != null && !oldIdVehiculeOfVisitetechniqueListNewVisitetechnique.equals(vehicule)) {
                        oldIdVehiculeOfVisitetechniqueListNewVisitetechnique.getVisitetechniqueList().remove(visitetechniqueListNewVisitetechnique);
                        oldIdVehiculeOfVisitetechniqueListNewVisitetechnique = em.merge(oldIdVehiculeOfVisitetechniqueListNewVisitetechnique);
                    }
                }
            }
            for (Panne panneListNewPanne : panneListNew) {
                if (!panneListOld.contains(panneListNewPanne)) {
                    Vehicule oldIdVehiculeOfPanneListNewPanne = panneListNewPanne.getIdVehicule();
                    panneListNewPanne.setIdVehicule(vehicule);
                    panneListNewPanne = em.merge(panneListNewPanne);
                    if (oldIdVehiculeOfPanneListNewPanne != null && !oldIdVehiculeOfPanneListNewPanne.equals(vehicule)) {
                        oldIdVehiculeOfPanneListNewPanne.getPanneList().remove(panneListNewPanne);
                        oldIdVehiculeOfPanneListNewPanne = em.merge(oldIdVehiculeOfPanneListNewPanne);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = vehicule.getId();
                if (findVehicule(id) == null) {
                    throw new NonexistentEntityException("The vehicule with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Vehicule vehicule;
            try {
                vehicule = em.getReference(Vehicule.class, id);
                vehicule.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The vehicule with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Assurance assuranceOrphanCheck = vehicule.getAssurance();
            if (assuranceOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Vehicule (" + vehicule + ") cannot be destroyed since the Assurance " + assuranceOrphanCheck + " in its assurance field has a non-nullable immatriculation field.");
            }
            List<Vignite> vigniteListOrphanCheck = vehicule.getVigniteList();
            for (Vignite vigniteListOrphanCheckVignite : vigniteListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Vehicule (" + vehicule + ") cannot be destroyed since the Vignite " + vigniteListOrphanCheckVignite + " in its vigniteList field has a non-nullable idVehicule field.");
            }
            List<Visitetechnique> visitetechniqueListOrphanCheck = vehicule.getVisitetechniqueList();
            for (Visitetechnique visitetechniqueListOrphanCheckVisitetechnique : visitetechniqueListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Vehicule (" + vehicule + ") cannot be destroyed since the Visitetechnique " + visitetechniqueListOrphanCheckVisitetechnique + " in its visitetechniqueList field has a non-nullable idVehicule field.");
            }
            List<Panne> panneListOrphanCheck = vehicule.getPanneList();
            for (Panne panneListOrphanCheckPanne : panneListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Vehicule (" + vehicule + ") cannot be destroyed since the Panne " + panneListOrphanCheckPanne + " in its panneList field has a non-nullable idVehicule field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Optionsupplimentaire idOption = vehicule.getIdOption();
            if (idOption != null) {
                idOption.getVehiculeList().remove(vehicule);
                idOption = em.merge(idOption);
            }
            Promotion idPromotion = vehicule.getIdPromotion();
            if (idPromotion != null) {
                idPromotion.getVehiculeList().remove(vehicule);
                idPromotion = em.merge(idPromotion);
            }
            em.remove(vehicule);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Vehicule> findVehiculeEntities() {
        return findVehiculeEntities(true, -1, -1);
    }

    public List<Vehicule> findVehiculeEntities(int maxResults, int firstResult) {
        return findVehiculeEntities(false, maxResults, firstResult);
    }

    private List<Vehicule> findVehiculeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Vehicule.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Vehicule findVehicule(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Vehicule.class, id);
        } finally {
            em.close();
        }
    }

    public int getVehiculeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Vehicule> rt = cq.from(Vehicule.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
